/**
 * 
 */
package com.xjt.common;

import java.io.Serializable;

/**
 * @author Jet Xu
 */
public class PageInfo implements Serializable {

    private static final long serialVersionUID = 6431336202626722106L;

    /** 默认每页显示10条 */
    private static final int DEFAULT_PAGE_SIZE = 10;

    /** 每页显示条数 */
    private int pageSize;

    /** 当前页数 */
    private int pageNo;

    /** 总页数 */
    private int pageCount;

    /** 总记录数 */
    private int totalCount;

    /** 第一页 */
    private int firstPage;

    /** 最后一页 */
    private int lastPage;

    /** 上一页 */
    private int prePage;

    /** 下一页 */
    private int nextPage;

    /** 需要获取条数 */
    private int limit;

    /** 偏移量 */
    private int offset;

    /** 排序方法 */
    private String orderByClause;

    /**
     * 计算页码信息
     * 
     * @param pageNo
     * @param totalCount
     */
    public void doPage(int pageNo, int totalCount) {

        this.totalCount = totalCount;
        // 如果当前页数小于等于0，则取第一个
        this.pageCount = (this.getTotalCount() - 1) / this.getPageSize() + 1;
        if (pageNo <= 0) {
            this.setPageNo(1);
        }
        if (pageNo > pageCount) {
            this.setPageNo(pageCount);
        }
        this.limit = this.getPageSize();
        this.offset = this.getPageSize() * (this.getPageNo() - 1);
    }

    /**
     * @return the pageSize
     */
    public int getPageSize() {
        if (pageSize <= 0) {
            pageSize = DEFAULT_PAGE_SIZE;
        }
        return pageSize;
    }

    /**
     * @param pageSize the pageSize to set
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * @return the pageCount
     */
    public int getPageCount() {
        return pageCount;
    }

    /**
     * @param pageCount the pageCount to set
     */
    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    /**
     * @return the firstPage
     */
    public int getFirstPage() {
        if (firstPage <= 0) {
            firstPage = 1;
        }
        return firstPage;
    }

    /**
     * @param firstPage the firstPage to set
     */
    public void setFirstPage(int firstPage) {
        this.firstPage = firstPage;
    }

    /**
     * @return the lastPage
     */
    public int getLastPage() {
        if (lastPage <= 0) {
            lastPage = this.getPageCount();
        }
        return lastPage;
    }

    /**
     * @param lastPage the lastPage to set
     */
    public void setLastPage(int lastPage) {
        this.lastPage = lastPage;
    }

    /**
     * @return the prePage
     */
    public int getPrePage() {
        prePage = this.getPageNo() - 1;
        if (prePage <= 0) {
            prePage = 1;
        }
        return prePage;
    }

    /**
     * @param prePage the prePage to set
     */
    public void setPrePage(int prePage) {
        this.prePage = prePage;
    }

    /**
     * @return the nextPage
     */
    public int getNextPage() {
        nextPage = this.getPageNo() + 1;
        if (nextPage <= 0 || nextPage > this.getPageCount()) {
            nextPage = this.getPageCount();
        }
        return nextPage;
    }

    /**
     * @param nextPage the nextPage to set
     */
    public void setNextPage(int nextPage) {
        this.nextPage = nextPage;
    }

    /**
     * @return the limit
     */
    public int getLimit() {
        if (limit <= 0) {
            limit = this.getPageSize();
        }
        return limit;
    }

    /**
     * @param limit the limit to set
     */
    public void setLimit(int limit) {
        this.limit = limit;
    }

    /**
     * @return the offset
     */
    public int getOffset() {
        if (offset < 0) {
            offset = 0;
        }
        return offset;
    }

    /**
     * @param offset the offset to set
     */
    public void setOffset(int offset) {
        this.offset = offset;
    }

    /**
     * @return the totalCount
     */
    public int getTotalCount() {
        if (totalCount < 0) {
            totalCount = 0;
        }
        return totalCount;
    }

    /**
     * @param totalCount the totalCount to set
     */
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * @return the pageNo
     */
    public int getPageNo() {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        return pageNo;
    }

    /**
     * @param pageNo the pageNo to set
     */
    public void setPageNo(int pageNo) {
        this.pageNo = pageNo;
    }

    /**
     * @return the orderByClause
     */
    public String getOrderByClause() {
        return orderByClause;
    }

    /**
     * @param orderByClause the orderByClause to set
     */
    public void setOrderByClause(String orderByClause) {
        this.orderByClause = orderByClause;
    }

}
